
//获取应用实例
var app = getApp();
//获得

var MyRequest = require("../../utils/MyRequest.js");
var Global = require("../../Global.js");
var util = require('../../utils/util.js');
var Cart = require("../../LocalCart.js");

Page({
  data: {
     firstMode: "active",
     secondMode: "",
     weipay:"",
  },
  onLoad: function(options) {
      this.setData({
        accountMoney: Global.toDecimal2(parseFloat(options.accountMoney)),
        freePrice: Global.toDecimal2(parseFloat(options.freePrice)),
        goodsCount: options.goodsCount,
        orderId: options.orderId,
        orderNo: options.orderNo,
        orderTime: options.orderTime,
        totalPrice: Global.toDecimal2(parseFloat(options.totalPrice))
      });
      this.Presenter.getPayBean(this);

      
  },
  Presenter: {
      
      getPayBean:function(that){
          var data = 
          { orderType: 1, 
            userId: Global.getUserStorage().id, 
            orderNo: that.data.orderNo, 
            money: that.data.totalPrice,
            cityId:Global.getUserStorage().cityId,
            platform:Global.getPlatform(),
            version:Global.getVersion(),
            wechatOpenid:Global.getOpenId(), 
            };
      MyRequest.MyGet("PayBean", that, that.Binder.bindPayBean, data);
      }
  },
  onReady: function() {
    
  },
  onShow: function() {
      
  },
  onHide: function() {
    
  },
  onUnload: function() {
    
  },
  onPullDownRefresh: function() {
    wx.stopPullDownRefresh();
  },
  onReachBottom: function() {
    
  },
 onFirstModeClick: function() {
      this.setData({
        firstMode: "active",
        secondMode: ""
      });
  },
   onSecondModeClick: function() {
      this.setData({
        firstMode: "",
        secondMode: "active"
      });
  },
  payOrder: function() {
      if(this.data.firstMode == "active") {
          var data = {
            action: "pay",
            orderNo: this.data.orderNo
          };
          MyRequest.MyGetWithToast("OrderPay",this, this.Binder.payOrderSuccess,  data)
      } else {
       
         wx.requestPayment({
            appId:this.data.weipay.appId,
            timeStamp:this.data.weipay.timeStamp,
            nonceStr:this.data.weipay.nonceStr,
            package:"prepay_id="+this.data.weipay.prepayId,
            signType:"MD5",
            paySign:this.data.weipay.sign,
            success:function(data){
                wx.redirectTo({
                  url: '/pages/orderList/orderList'
                })
            },fail:function(data){
               wx.showToast({ title: '支付失败,请稍后再试', icon: 'loading', duration: 2000 })
            },complete:function(data){
                console.log(data);
            }
         })
      }
  },
  Binder: {
      payOrderSuccess: function(result, that) {         
          wx.redirectTo({
              url: '/pages/orderList/orderList'
          })
      },
      bindPayBean:function(result,that){
          that.setData({
              weipay:result.data.data.weiPay
          })
    
      }
  }
})